package cn.hairui.blog.controller.manage;

import cn.hairui.blog.constant.PubConstant;
import cn.hairui.blog.model.TabMenu;
import cn.hairui.blog.model.User;
import cn.hairui.blog.service.TabMenuService;
import com.alibaba.druid.support.json.JSONUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @author lihairui
 * @version V1.0
 * @date: 2019/8/10 17:36
 */
@Controller
@RequestMapping(value = "/manage")
public class TabMenuManageController {
    @Autowired
    private TabMenuService tabMenuService;

    //列表页面
    @RequestMapping(value = "page/tabMenu/tabMenuList")
    public String tabMenuList() {
        return "manage/page/tabMenu/tabMenuList";
    }

    //新增页面
    @RequestMapping(value = "page/tabMenu/tabMenuAdd")
    public String tabMenuAdd() {
        return "manage/page/tabMenu/tabMenuAdd";
    }

    //修改页面
    @RequestMapping(value = "page/tabMenu/tabMenuUpdate")
    public String tabMenuUpdate(HttpServletRequest request, Model model) {
        int id = Integer.parseInt(request.getParameter("id"));
        TabMenu tabMenu = tabMenuService.find(id);
        model.addAttribute("tabMenu", tabMenu);
        return "manage/page/tabMenu/tabMenuUpdate";
    }


    //列表数据
    @RequestMapping(value = "page/tabMenu/list")
    @ResponseBody
    public String list(HttpServletRequest request) {
        Map map = new HashMap<Object, Object>();
        int pageSize = 10;//默认每页显示10条记录
        int pageNum = 1;
        try {
            pageNum = Integer.parseInt(request.getParameter("page"));
            pageSize = Integer.parseInt(request.getParameter("limit"));
        } catch (Exception e) {
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Map> tabMenuList = tabMenuService.listMgr();
        PageInfo<Map> pageInfo = new PageInfo<Map>(tabMenuList);
        map.put("data", tabMenuList);
        map.put("code", 0);
        map.put("count", pageInfo.getTotal());
        System.out.println(tabMenuList);
        return JSONUtils.toJSONString(map);
    }

    //新增数据
    @RequestMapping(value = "page/tabMenu/add")
    @ResponseBody
    public String add(HttpServletRequest request, TabMenu tabMenu) {
        System.out.println(tabMenu.toString());
        Map map = new HashMap<Object, Object>();
        //处理分类置顶
        if (PubConstant.SWITCH_ON.equals(tabMenu.getShowHomepage())) {
            int n = tabMenuService.listShow().size();
            if (n < PubConstant.MAX_SHOW_NAVMENU) {
                tabMenu.setShowHomepage(PubConstant.YES_NO_Y);
            } else {
                map.put(PubConstant.flag, "max");
                return JSONUtils.toJSONString(map);
            }

        } else {
            tabMenu.setShowHomepage(PubConstant.YES_NO_N);
        }
        int serno = tabMenuService.add(tabMenu);
        Logger.getLogger(this.getClass().getName()).info("新增首页分类编号：" + serno);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

    //更新数据
    @RequestMapping(value = "page/tabMenu/update")
    @ResponseBody
    public String update(HttpServletRequest request, TabMenu tabMenu) {
        System.out.println(tabMenu.toString());
        Map map = new HashMap<Object, Object>();
        //处理专题编号
        if (PubConstant.SWITCH_ON.equals(tabMenu.getShowHomepage())) {

            int n = tabMenuService.listShow().size();
            if (n < PubConstant.MAX_SHOW_NAVMENU) {
                tabMenu.setShowHomepage(PubConstant.YES_NO_Y);
            } else {
                map.put(PubConstant.flag, "max");
                return JSONUtils.toJSONString(map);
            }
        } else {
            tabMenu.setShowHomepage(PubConstant.YES_NO_N);
        }
        tabMenuService.update(tabMenu);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

    //删除数据
    @RequestMapping(value = "page/tabMenu/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(@Param(value = "id") int id, HttpServletRequest request) {
        Map map = new HashMap();
        //判断权限
        User userinfo = (User) request.getSession().getAttribute(PubConstant.GLOBAL_SESSION_NAME);
        if (userinfo == null || !PubConstant.YES_NO_Y.equals(userinfo.getAdminflag())) {
            //普通用户或者游客 无权限删除文章
            map.put(PubConstant.flag, PubConstant.permissions);
        } else {
            //管理员
            tabMenuService.delete(id);
            map.put(PubConstant.flag, PubConstant.success);
        }
        return JSONUtils.toJSONString(map);
    }

    //首页展示
    @RequestMapping(value = "page/tabMenu/tabMenushow")
    @ResponseBody
    public String tabMenushow(HttpServletRequest request) {
        Map map = new HashMap<Object, Object>();
        int id = Integer.parseInt(request.getParameter("id"));
        TabMenu tabMenu = tabMenuService.find(id);
        System.out.println(tabMenu.toString());
        String flag = "";
        if (PubConstant.YES_NO_Y.equals(tabMenu.getShowHomepage())) {
            //取消展示
            flag = PubConstant.YES_NO_N;

        } else {
            //首页展示
            //获取当前最大展示数量
            int n = tabMenuService.listShow().size();
            if (n < PubConstant.MAX_SHOW_NAVMENU) {
                flag = PubConstant.YES_NO_Y;
            } else {
                map.put(PubConstant.flag, "max");
                return JSONUtils.toJSONString(map);
            }

        }
        tabMenuService.updateShowFlag(id, flag);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

}
